package main;

import gui.*;
import player.*;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;


public class mainEntry{

	
	//Start Main Program here//
	public static void main( String[] args )
	{
		if(args[0] == null){
			System.err.println("Please enter IP and port numbers.");
			System.exit(1);
		}
		String[] temp1 = args[0].split(":"); //inputing host and port from command line
		String tempHost = null;
		int tempPort = 0;
		try{
			tempHost = temp1[0];
			clientToServer.getInstance().setServerHost(InetAddress.getByName(temp1[0]));
		}
		catch(ArrayIndexOutOfBoundsException e){
			System.out.println("No host IP specified. Be sure to specify one before connecting.");
		}
		catch(IOException e1){
			e1.printStackTrace();
		}
		try{
			tempPort = Integer.parseInt(temp1[1]);
			while(!checkPort(tempPort)){
			      Scanner in = new Scanner(System.in);
			      System.out.println("Please enter a port between 1024 and 65,000 (default is 5000).\n");
			      tempPort = in.nextInt();
			}
			clientToServer.getInstance().setPort(Integer.parseInt(temp1[1]));
		}
		catch(ArrayIndexOutOfBoundsException e){
			System.out.println("No port specified, continuing with default port 5000.");
		}
		
		
		mainGui MAIN = mainGui.getInstance();
		mainGui.getInstance().setHostInput(tempHost); //setting GUI host according to input
		MAIN.display();
		System.out.println("GUI initialized.");
		@SuppressWarnings("unused")
		player PLAYER = player.getInstance();
		System.out.println("New player class created.");
		msgHandler MSGHANDLER = msgHandler.getInstance();
		MSGHANDLER.start();
		System.out.println("New message handler class created.");
		@SuppressWarnings("unused")
		popupHandler POPPER = popupHandler.getInstance();
		System.out.println("New popup handler class created.");
     
	}
	
	public static boolean checkPort(int port){
		if(port>1024 && port<65000){
			return true;
		}
		else{
			return false;
		}
	}
}



/*testing:
 *              Manipulator man = new Manipulator();
		Scrambler theChanger = new Scrambler();
		
		String testString = "Rocky";
		
		String[] test = man.str2arr(testString);
		String[] scrambled = new String[test.length];
		for ( int i=0 ; i<test.length ; i++ ){
			scrambled[i] = theChanger.scrambleWord(test[i]);
		}
		
		for ( int i=0 ; i<test.length ; i++ ){
		System.out.println( scrambled[i] );
		}
 */